﻿using AutoMapper;
using B.S.Filtration.Domain.到货检验;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Read.Api.Application.Command.到货检验.PurchaseInfoCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Read.Api.Application.CommandHander.到货检验.PurchaseInfoHandler
{
    public class GetPurchaseInfoHandler : IRequestHandler<GetPurchaseInfoCommand, APIResult<APIPaging<PurchaseInfo>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;
        public GetPurchaseInfoHandler(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }
        public async Task<APIResult<APIPaging<PurchaseInfo>>> Handle(GetPurchaseInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<PurchaseInfo>> result = new APIResult<APIPaging<PurchaseInfo>>();
            var model = await db.Queryable<PurchaseInfo>().Where(x => !x.IsDel).ToListAsync();

            int totalCount = model.Count();
            int PageCount = (int)Math.Ceiling(totalCount * 1.0 / request.pageSize);
            model = model.OrderByDescending(a => a.BasicInfoid).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            APIPaging<PurchaseInfo> paging = new APIPaging<PurchaseInfo>()
            {
                TotalCount = totalCount,
                TotalPage = PageCount,
                list = model.ToList()
            };
            result.code = APIResultCode.success;
            result.msg = "显示成功";
            result.data = paging;
            return result;

        }
    }
}
